package com.czy.owner.widget.timersection;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.widget.timersection.listener.OnSectionSelListener;
import com.jzxiang.pickerview.config.PickerConfig;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.data.WheelCalendar;

import java.util.Calendar;

/**
 * @author: Raven
 * @date: 2018/4/9 14:02
 * @email: xiexiaomeng_001@163.com
 * @description:
 * 日期区间选择对话框，继承TimerPicker控件相关类来， 时间问题，未做完全脱离，只满足当前需要，所以部分多余属性会无效。
 * 未做开始时间与结束时间的动态判定：即滚动到开始时间大于结束时间的时候自动调整结束时间。
 */

public class TimerSectionDialog extends DialogFragment implements View.OnClickListener {


    private SectionConfig mSectionConfig;
    private SectionWheel mSectionWheel;
    private long mStartMillSeconds;
    private long mEndMillSeconds;

    private static TimerSectionDialog newIntance(SectionConfig config) {
        TimerSectionDialog timePickerDialog = new TimerSectionDialog();
        timePickerDialog.initialize(config);
        return timePickerDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activity activity = getActivity();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    }

    @Override
    public void onResume() {
        super.onResume();
        int height = getResources().getDimensionPixelSize(R.dimen.picker_height);
        DisplayMetrics dm2 = getResources().getDisplayMetrics();
        int width =dm2.widthPixels * 90/100;
        Window window = getDialog().getWindow();
        window.setLayout(width, height);//Here!
        window.setGravity(Gravity.CENTER);
    }

    private void initialize(PickerConfig pickerConfig) {
        mSectionConfig = (SectionConfig) pickerConfig;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.DialogTimerSection);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(initView());
        return dialog;
    }

    View initView() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.timer_section_layout, null);
        TextView cancel = (TextView) view.findViewById(R.id.tv_cancel);
        cancel.setOnClickListener(this);
        TextView sure = (TextView) view.findViewById(R.id.tv_sure);
        sure.setOnClickListener(this);
        TextView title = (TextView) view.findViewById(R.id.tv_title);

        title.setText(mSectionConfig.mTitleString);
        cancel.setText(mSectionConfig.mCancelString);
        sure.setText(mSectionConfig.mSureString);
//        title.setBackgroundColor(mSectionConfig.mThemeColor);
        cancel.setTextColor(mSectionConfig.mThemeColor);
        sure.setTextColor(mSectionConfig.mThemeColor);
        title.setTextColor(mSectionConfig.mThemeColor);
        mSectionWheel = new SectionWheel(view, mSectionConfig);
        return view;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_cancel) {
            dismiss();
        } else if (i == R.id.tv_sure) {
            sureClicked();
        }
    }

    void sureClicked() {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();

        calendar.set(Calendar.YEAR, mSectionWheel.getCurrentStartYear());
        calendar.set(Calendar.MONTH, mSectionWheel.getCurrentStartMonth() - 1);
        calendar.set(Calendar.DAY_OF_MONTH, mSectionWheel.getCurrentStartDay());
        Log.d("TimePicker"," start calendar="+calendar.toString());
        mStartMillSeconds = calendar.getTimeInMillis();
        Log.d("TimePicker","start mStartMillSeconds="+mStartMillSeconds);



        calendar.set(Calendar.YEAR, mSectionWheel.getCurrentEndYear());
        calendar.set(Calendar.MONTH, mSectionWheel.getCurrentEndMonth() - 1);
        calendar.set(Calendar.DAY_OF_MONTH, mSectionWheel.getCurrentEndDay());
        Log.d("TimePicker"," end calendar="+calendar.toString());
        mEndMillSeconds = calendar.getTimeInMillis();
        Log.d("TimePicker","end mEndMillSeconds="+mEndMillSeconds);
        if(mStartMillSeconds>mEndMillSeconds){
            PhoneUtils.ShowToastMessage(getContext(),"对不起，您选择的日期区间不合法");
            return;
        }
        if (mSectionConfig.onSectionSelListener != null) {
            mSectionConfig.onSectionSelListener.onDateSet(this, mStartMillSeconds,mEndMillSeconds);
        }
        dismiss();
    }


    public static class Builder {
        SectionConfig mPickerConfig;

        public Builder() {
            mPickerConfig = new SectionConfig();
        }

        public Builder setType(Type type) {
            mPickerConfig.mType = type;
            return this;
        }

        public Builder setThemeColor(int color) {
            mPickerConfig.mThemeColor = color;
            return this;
        }

        public Builder setCancelStringId(String left) {
            mPickerConfig.mCancelString = left;
            return this;
        }

        public Builder setSureStringId(String right) {
            mPickerConfig.mSureString = right;
            return this;
        }

        public Builder setTitleStringId(String title) {
            mPickerConfig.mTitleString = title;
            return this;
        }

        public Builder setToolBarTextColor(int color) {
            mPickerConfig.mToolBarTVColor = color;
            return this;
        }

        public Builder setWheelItemTextNormalColor(int color) {
            mPickerConfig.mWheelTVNormalColor = color;
            return this;
        }

        public Builder setWheelItemTextSelectorColor(int color) {
            mPickerConfig.mWheelTVSelectorColor = color;
            return this;
        }

        public Builder setWheelItemTextSize(int size) {
            mPickerConfig.mWheelTVSize = size;
            return this;
        }

        public Builder setCyclic(boolean cyclic) {
            mPickerConfig.cyclic = cyclic;
            return this;
        }

        public Builder setMinMillseconds(long millseconds) {
            mPickerConfig.mMinCalendar = new WheelCalendar(millseconds);
            return this;
        }

        public Builder setMaxMillseconds(long millseconds) {
            mPickerConfig.mMaxCalendar = new WheelCalendar(millseconds);
            return this;
        }

        public Builder setCurrentStartMillseconds(long millseconds) {
            mPickerConfig.mCurrentStartCalendar = new WheelCalendar(millseconds);
            return this;
        }

        public Builder setCurrentEndMillseconds(long millseconds) {
            mPickerConfig.mCurrentEndCalendar = new WheelCalendar(millseconds);
            return this;
        }

        public Builder setYearText(String year){
            mPickerConfig.mYear = year;
            return this;
        }

        public Builder setMonthText(String month){
            mPickerConfig.mMonth = month;
            return this;
        }

        public Builder setDayText(String day){
            mPickerConfig.mDay = day;
            return this;
        }

        public Builder setHourText(String hour){
            mPickerConfig.mHour = hour;
            return this;
        }

        public Builder setMinuteText(String minute){
            mPickerConfig.mMinute = minute;
            return this;
        }

        public Builder setCallBack(OnSectionSelListener listener) {
            mPickerConfig.onSectionSelListener = listener;
            return this;
        }

        public TimerSectionDialog build() {
            return newIntance(mPickerConfig);
        }

    }
}
